package com.studentstreet;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.studentstreet.component.LwtAlertDialog;
import com.studentstreet.tools.HttpHelper;
import com.studentstreet.tools.Util;

public class CommentActivity extends Activity {
	// 与页头部分有关
	// 登录按钮
	private Button loginBt = null;
	// 页面标题
	private TextView title = null;
	// 返回按钮
	private ImageButton goBackBt = null;

	// progressBar
	private ProgressDialog progressDialog = null;
	// 评论标题
	private EditText titleEt = null;
	// 评论内容
	private EditText contentEt = null;
	// 打分
	private RatingBar scoreBar = null;
	// 提交评论按钮
	private Button commitBt = null;

	// 商品号
	private String goodsId = "";
	// 用户名
	private String userName = "";

	// 标记用户是否购买了该商品
	private final static int NOT_BUY = 0;
	private final static int HAVE_BUY = 1;
	// 评论成功
	private final static int ADD_SUCCESS = 2;
	// 评论失败
	private final static int ADD_FAILED = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置页面无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//将当前Activity加入到activity队列中
		MyApp.getInstance().addActivity(this);
		//设置布局
		setContentView(R.layout.add_comment);
		//获得上个Activity传来的数据
		Intent intent = getIntent();
		userName = intent.getStringExtra("userName");
		goodsId = intent.getStringExtra("goodsId");

		//初始化布局
		initView();
		//启动查询用户是否购买了该商品的线程。
		start();
	}

	private void initView() {
		// 初始化标题部分
		// 登录按钮
		loginBt = (Button) findViewById(R.id.login_bt);
		loginBt.setBackgroundDrawable(null);
		loginBt.setText(userName);

		// 返回按钮
		goBackBt = (ImageButton) findViewById(R.id.header_imgBt);
		goBackBt.setImageDrawable(getResources().getDrawable(R.drawable.back));
		goBackBt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// 页面的标题
		title = (TextView) findViewById(R.id.title);
		title.setText(getString(R.string.add_goods_comment));

		// 初始化评论信息
		// 评论标题
		titleEt = (EditText) findViewById(R.id.comment_title_et);
		// 评论内容
		contentEt = (EditText) findViewById(R.id.comment_content_et);
		// 评分
		scoreBar = (RatingBar) findViewById(R.id.comment_score_rb);
		// 提交按钮
		commitBt = (Button) findViewById(R.id.comment_add_bt);

		commitBt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("".equals(titleEt.getText().toString().trim())) {
					showDialog("评论标题必须输入！", false);
					return;
				}
				if ("".equals(contentEt.getText().toString().trim())) {
					showDialog("评论内容必须输入！", false);
					return;
				}
				commitComment();
			}
		});
	}

	/**
	 * 添加评论的线程
	 */
	private void commitComment() {
		if(progressDialog!=null&&!progressDialog.isShowing()){
			progressDialog.show();
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				String result = "";
				// 从服务器端下载商品Json数据
				HttpHelper helper = new HttpHelper(Util.BASE_URL
						+ "/servlet/CommentServlet");
				// 添加参数
				helper.addParam("action", "add");
				helper.addParam("goods_id", goodsId);
				helper.addParam("userName", userName);
				helper.addParam("title", titleEt.getText().toString());
				helper.addParam("content", contentEt.getText().toString());
				helper.addParam("score", scoreBar.getRating() + "");

				result = helper.getResponse();
				if ("success".equals(result)) {
					handler.sendEmptyMessage(ADD_SUCCESS);
				} else {
					handler.sendEmptyMessage(ADD_FAILED);
				}
			}
		}).start();
	}

	/**
	 * 用于启动用户是否购买了该商品的线程
	 */
	private void start() {
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getString(R.string.add_comment_loading));
		progressDialog.show();
		//启动查询线程
		isCommentted();
	}

	/**
	 * 查询用户是否购买了该商品的线程
	 */
	private void isCommentted() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String result = "";
				// 从服务器端下载商品Json数据
				HttpHelper helper = new HttpHelper(Util.BASE_URL
						+ "/servlet/CommentServlet");
				// 添加参数
				helper.addParam("action", "confirm");
				helper.addParam("goods_id", goodsId);
				helper.addParam("userName", userName);

				result = helper.getResponse();
				//如果结果为success表示用户已经购买了该商品，可以写评论
				if ("success".equals(result)) {
					handler.sendEmptyMessage(HAVE_BUY);
				} else {//用户没有购买该商品
					handler.sendEmptyMessage(NOT_BUY);
				}
			}
		}).start();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			progressDialog.dismiss();
			switch (msg.what) {
			case HAVE_BUY://用户购买过该商品
				break;
			case NOT_BUY://用户没有购买该商品
				//显示对话框，当用户点击确定按钮后finish当前Activity
				showDialog("你还没有买过该商品，不能评论", true);
				break;
			case ADD_SUCCESS:
				//添加评论成功，当用户点击确定按钮后finish当前Activity
				showDialog("评论商品成功", true);
				break;
			case ADD_FAILED:
				//评论添加失败，当用户点击确定按钮后不finish当前Activity
				showDialog("评论商品失败，请重试", false);
				break;
			default:
				break;
			}
		}
	};

	
	/**
	 * 显示一个Alertdialog，提示用户一些信息
	 * @param msg 要显示的提示信息
	 * @param isFinish  当用户点击确定按钮以后，是否结束当前activity
	 */
	private void showDialog(String msg, final boolean isFinish) {
		LwtAlertDialog.Builder builder = new LwtAlertDialog.Builder(this);
		builder.setTitle(android.R.string.dialog_alert_title)
				.setMessage(msg)
				.setNegativeButton(R.string.sure,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (isFinish) {
									finish();
								} else {
									dialog.dismiss();
								}
							}
						});
		Dialog d = builder.create();
		d.show();
	}
}
